package com.zzl.study.auth.security.authentication.client;

import lombok.Data;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;

import java.time.Instant;

/**
 * @author: zhangzl
 * @date: 2024/6/11 15:53
 * @version: 1.0
 * @description: OAuth2 refresh_token
 */
@Data
public class GatewayOAuth2RefreshToken {

    private String tokenValue;

    private Instant issuedAt;

    private Instant expiresAt;

    public static GatewayOAuth2RefreshToken of(OAuth2RefreshToken oAuth2RefreshToken) {
        if (oAuth2RefreshToken == null) {
            return null;
        }
        GatewayOAuth2RefreshToken gatewayOAuth2RefreshToken = new GatewayOAuth2RefreshToken();
        gatewayOAuth2RefreshToken.setTokenValue(oAuth2RefreshToken.getTokenValue());
        gatewayOAuth2RefreshToken.setIssuedAt(oAuth2RefreshToken.getIssuedAt());
        gatewayOAuth2RefreshToken.setExpiresAt(oAuth2RefreshToken.getExpiresAt());
        return gatewayOAuth2RefreshToken;
    }

    public static OAuth2RefreshToken to(GatewayOAuth2RefreshToken gatewayOAuth2RefreshToken) {
        if (gatewayOAuth2RefreshToken == null) {
            return null;
        }
        return new OAuth2RefreshToken(
                gatewayOAuth2RefreshToken.getTokenValue(),
                gatewayOAuth2RefreshToken.getIssuedAt(),
                gatewayOAuth2RefreshToken.getExpiresAt());
    }

}
